<template>
  <div class="bottom-bar">
    <div class="bar-selector">
      <check-button class="check-button" 
                    :is-checked="isSelectAll" 
                    @click.native="checkClick"/>
      <span>全选</span>
    </div>
    <div class="bar-total">
      合计：{{totalPlayCount}}
    </div>
    <div class="bar-calculate" @click="calcClick">
      去结算({{checkLength}})
    </div>
  </div>
</template>

<script>
  import CheckButton from 'components/content/checkButton/CheckButton'

  import {mapGetters} from 'vuex'

  export default {
    name: 'CartBottomBar',
    components: {
      CheckButton
    },
    computed: {
      ...mapGetters(['cartList']),
      totalPlayCount() {
        return '￥' + this.cartList.filter(item => item.checked).reduce((preValue, item) => {
          return preValue + item.playCount * item.count
        }, 0).toFixed(2)
      },
      checkLength() {
        return this.cartList.filter(item => item.checked).length
      },
      isSelectAll() {
        if (this.cartList.length === 0) return false
        // return !(this.cartList.filter(item => !item.checked).length)
        // return !this.cartList.find(item => !item.checked)
        for (let item of this.cartList) {
          if (!item.checked) {
            return false
          }
        }
        return true
      }
    },
    methods: {
      checkClick() {
        /* if (this.isSelectAll) {  // 全部选中 点击后 变为全不选
          this.cartList.forEach(item => item.checked = false)
        } else {  // 全部不选中 点击后 变为全选
          this.cartList.forEach(item => item.checked = true)
        } */
        let isSelectAll = this.isSelectAll
        this.cartList.forEach(item => item.checked = !isSelectAll)
      },
      calcClick() {
        if(this.cartList.filter(item => item.checked).length === 0) {
          this.$toast.show('请选择购买的商品', 2000)
        }
      }
    }
  }
</script>

<style scoped>
  .bottom-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: absolute;
    bottom: 56px;
    left: 0;
    right: 0;
    height: 40px;
    background-color: #eee;
  }

  .bar-selector {
    display: flex;
    transform: scale(0.8);
    margin-top: 5px;
  }

  .check-button {
    width: 20px;
    height: 20px;
    margin: -2px 5px 0 10px;
  }

  .bar-total {
    /* margin-left: -30px; */
    width: 50%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .bar-calculate {
    height: 100%;
    line-height: 40px;
    background-color: var(--color-tint);
    color: #fff;
    padding: 0 5px;
  }
</style>